/*
 * Copyright [2012] [Robert James Szabo]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package com.sababado.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * Android device related Utility functions
 * @author Robert J. Szabo
 * @since 08/27/2012
 * @version 1.0
 */
public class UtilDevice
{
	public static final boolean IS_PRE_HONEYCOMB = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
	/**
	 * Request to hide the soft input window from the context of the window that is currently accepting input.
	 * @param context
	 * @param view The view that has keyboard focus
	 */
	public static void hideKeyboard(Context context, View view)
	{
		InputMethodManager imm = (InputMethodManager)(context.getSystemService(Context.INPUT_METHOD_SERVICE));
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
	
	/**
	 * Request that the current input method's soft input area be shown to the user, if needed.
	 * @param context
	 * @param view The view that should get keyboard focus
	 */
	public static void showKeyboard(Context context, View view)
	{
		InputMethodManager imm = (InputMethodManager)(context.getSystemService(Context.INPUT_METHOD_SERVICE));
		imm.showSoftInput(view, 0);
	}
	
	/**
	 * Get the density pixel (dp) measurement of the width of this device
	 * @param activity The activity that is invoking this method
	 * @return density pixels as an integer
	 */
	@SuppressWarnings ("deprecation")
	public static int getDeviceDpWidth(Activity activity)
	{
		DisplayMetrics metrics = new DisplayMetrics();
        Display display = activity.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        float logicalDensity = metrics.density;
        int dp= (int)(display.getWidth() / logicalDensity +0.5);
        return dp;
	}
	
	/**
	 * Get the density pixel (dp) measurement of the height of this device
	 * @param activity The activity that is invoking this method
	 * @return density pixels as an integer
	 */
	@SuppressWarnings ("deprecation")
	public static int getDeviceDpHeight(Activity activity)
	{
		DisplayMetrics metrics = new DisplayMetrics();
        Display display = activity.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        float logicalDensity = metrics.density;
        int dp= (int)(display.getHeight() / logicalDensity +0.5);
        return dp;
	}
	
	/**
	 * Get the density pixel (dp) measurement of the width and height of this device
	 * @param activity The activity that is invoking this method
	 * @return density pixels as an integer array [width, height]
	 */
	@SuppressWarnings ("deprecation")
	public static int[] getDeviceDpDimensions(Activity activity)
	{
		DisplayMetrics metrics = new DisplayMetrics();
        Display display = activity.getWindowManager().getDefaultDisplay();
        display.getMetrics(metrics);
        float logicalDensity = metrics.density;
        int dpWidth= (int)(display.getWidth() / logicalDensity +0.5);
        int dpHeight= (int)(display.getHeight() / logicalDensity +0.5);
        return new int[]{dpWidth,dpHeight};
	}
}
